



//
//  ZSubReadHistoryViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/5/7.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZSubReadHistoryViewController.h"
#import "BookHistoryCell.h"
#import "ReadModel.h"
@interface ZSubReadHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger index;
@end

@implementation ZSubReadHistoryViewController

#define cellIdentifier @"bookhis"


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 3, ScreenWidth , ScreenHeight - kTabHeight-ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 100.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"BookHistoryCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
//        WeakStament(ws);
//        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
//            [ws refreshData];
//        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSMutableArray array];
    self.index = -1;
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}


#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadModel *model = self.dataArray[indexPath.row];
    BookHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    YYLog(@"%@",model.outsideInfo[@"booInfo"][@"photo"]);
    if (self.index==0) {
        cell.titleLab.text = model.name;
        cell.timeLab.text = model.createTime;
        /// 68 90
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]] placeholderImage:nil];
    }else{
        
        cell.imgWidth.constant = 125.f;
        cell.imgHeight.constant = 112;
        cell.titleLab.text = model.name;
        cell.timeLab.text = model.createTime;
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]] placeholderImage:nil];
        
    }
    
    return cell;
}

#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    YYLog(@"%ld",index);
//    [UserModel shareInstance].tabIndex = index;
        self.index = index;
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    YYLog(@"%ld",index);
    //    self.currentIndex = index;
    //    [self loadDataWithIndex:index];
    //    [self loadDataWithIndex:[UserModel shareInstance].tabIndex];
    [self loadDataWithIndex:index];
}

-(void)loadDataWithIndex:(NSInteger)index{
    
    ///历史阅读接口变化了，需要进行相关的调整
    NSString *userid = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    YYLog(@"%@",userid);
//    if (index==0) {
    ///一定要弄清赋值和等于的不同，index==0  注意
    NSDictionary *dic = @{@"pageIndex":@"1",@"pageSize":@"50",@"userInfo.id":userid,@"recordType":(index==0)?@"1":@"2",};
//    }
    [ZNetworkService postRequestWithBaseUrl:kHistoryRecordUrl setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            if (arr.count==0) {
                [AFToast showText:@"当前数据为空"];
                return ;
            }
//            NSMutableArray *temp = [NSMutableArray arrayWithArray:arr];
//            [temp removeObjectAtIndex:0];
//            YYLog(@"%ld,%ld",temp.count,arr.count);
            for (NSDictionary *dict in arr) {
                ReadModel *model = [ReadModel mj_objectWithKeyValues:dict];
                ///注意key 中字母的大小写  特别重要，最好直接粘贴
                if (index==0) {
                    ///shuj
                    model.photo = model.bookInfo[@"photo"];
                    model.name = model.bookInfo[@"name"];
                    model.createTime = model.bookInfo[@"createTime"];
                    
                    
                }else{
                    ///视频 因为对应的model 中字段不同
                    //                    NSDictionary *subDic = model.outsideInfo[@"bookInfo"];
                    model.photo = model.videoInfo[@"photo"];
                    model.name = model.videoInfo[@"name"];
                    model.ID = model.videoInfo[@"id"];
                    model.createTime = model.videoInfo[@"createTime"];
                    YYLog(@"%@",model.photo);
                }
                
                [self.dataArray addObject:model];
            }
            
            [self.tableView reloadData];
        }
        
    } fail:^(NSError *error) {
        
    } delegater:nil];
}
@end
